package com.fubang.rxjavalearning.filterOperator;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.fubang.rxjavalearning.R;
import com.fubang.rxjavalearning.mapOperator.House;

import java.util.ArrayList;

import rx.Observable;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.functions.Func2;

public class FilterActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_filter);
        //filter操作符
        Observable<String> scan = Observable.just("1", "2", "3", "4", "5", "6", "7")
                .scan(new Func2<String, String, String>() {
                    @Override
                    public String call(String s, String s2) {
                        return s + s2;
                    }
                });
        scan
                .filter(new Func1<String, Boolean>() {
                    @Override
                    public Boolean call(String s) {
                        return s.length() < 5;
                    }
                }).subscribe(new Action1<String>() {
            @Override
            public void call(String s) {
                System.out.println(s);
            }
        });
        System.out.println("-----------------------------------");
        //take操作符
        scan.take(2)
                .subscribe(new Action1<String>() {
                    @Override
                    public void call(String s) {
                        System.out.println(s);
                    }
                });
        System.out.println("-----------------------------------");
        //takeLast
        scan.takeLast(3)
                .subscribe(new Action1<String>() {
                    @Override
                    public void call(String s) {
                        System.out.println(s);
                    }
                });
        System.out.println("-----------------------------------");
        //takeUntil
        Observable.just(1, 2, 3, 4, 5, 6, 7)
                .takeUntil(new Func1<Integer, Boolean>() {
                    @Override
                    public Boolean call(Integer integer) {
                        return integer > 4;
                    }
                }).subscribe(new Action1<Integer>() {
            @Override
            public void call(Integer integer) {
                System.out.println(integer + "");
            }
        });
        System.out.println("-----------------------------------");
        //skip
        scan.skip(4)
                .subscribe(new Action1<String>() {
                    @Override
                    public void call(String s) {
                        System.out.println(s);
                    }
                });
        System.out.println("-----------------------------------");

        //elementAt
        scan.elementAt(4)
                .subscribe(new Action1<String>() {
                    @Override
                    public void call(String s) {
                        System.out.println(s);
                    }
                });
        System.out.println("-----------------------------------");
        //takeUntil(Observable)   不懂
//        Observable<Long> observableA = Observable.interval(300, TimeUnit.MILLISECONDS);
//        Observable<Long> observableB = Observable.interval(800, TimeUnit.MILLISECONDS);
//
//        observableA.takeUntil(observableB)
//                .subscribe(new Subscriber<Long>() {
//                    @Override
//                    public void onCompleted() {
//                        System.exit(0);
//                    }
//
//                    @Override
//                    public void onError(Throwable e) {
//
//                    }
//
//                    @Override
//                    public void onNext(Long aLong) {
//                        System.out.println(aLong);
//                    }
//                });
//
//        try {
//            Thread.sleep(Integer.MAX_VALUE);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
        System.out.println("-----------------------------------");
        //distinct
        Observable<Integer> just = Observable.just(2, 1, 2, 2, 3, 4, 3, 4, 5, 5);
        just
                .distinct()
                .subscribe(new Action1<Integer>() {
                    @Override
                    public void call(Integer integer) {
                        System.out.println(integer + "");
                    }
                });
        System.out.println("-----------------------------------");

        //distinct(Func1)
        ArrayList<House> houses = new ArrayList<>();
        houses.add(new House("中粮·海景壹号", "中粮海景壹号新出大平层！总价4500W起"));
        houses.add(new House("竹园新村", "满五唯一，黄金地段"));
        houses.add(new House("中粮·海景壹号", "毗邻汤臣一品"));
        houses.add(new House("竹园新村", "顶层户型，两室一厅"));
        houses.add(new House("中粮·海景壹号", "南北通透，豪华五房"));

        Observable<House> from = Observable.from(houses);
        from
                .distinct(new Func1<House, String>() {
                    @Override
                    public String call(House house) {
                        return house.getName();
                    }
                }).subscribe(new Action1<House>() {
            @Override
            public void call(House house) {
                System.out.println("小区：" + house.getName() + " 描述：" + house.getDesc());
            }
        });

        System.out.println("-----------------------------------");
        //distinctUntilChanged
        just.distinctUntilChanged()
                .subscribe(new Action1<Integer>() {
                    @Override
                    public void call(Integer integer) {
                        System.out.println(integer + "");
                    }
                });
        System.out.println("-----------------------------------");
        //distinctUntilChanged(Func1)
        from.distinctUntilChanged(new Func1<House, String>() {
            @Override
            public String call(House house) {
                return house.getName();
            }
        }).subscribe(new Action1<House>() {
            @Override
            public void call(House house) {
                System.out.println("小区：" + house.getName() + " 描述：" + house.getDesc());
            }
        });
        System.out.println("-----------------------------------");
        //first
        just.first().subscribe(new Action1<Integer>() {
            @Override
            public void call(Integer integer) {
                System.out.println(integer+"");
            }
        });
        System.out.println("-----------------------------------");
        //first(Func1)
        from.first(new Func1<House, Boolean>() {
            @Override
            public Boolean call(House house) {
                return house.getDesc().contains("汤臣一品");
            }
        }).subscribe(new Action1<House>() {
            @Override
            public void call(House house) {
                System.out.println("小区：" + house.getName() + " 描述：" + house.getDesc());
            }
        });
        System.out.println("-----------------------------------");
    }
}
